<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Award extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/home
	 *	- or -  
	 * 		http://example.com/index.php/home/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    function __construct()
    {
        parent::__construct(); 
		
		//Google Analytics
		$this->data['ga_id'] = 17; //award.you2play.com
		$this->data['ga_event'] = "You2Play Award"; //www.you2play.com		
				
		//Set Cache
		//$this->output->cache(2);
		//Load Database
		$this->DB_AWARD = $this->load->database("you2play_award_2014",TRUE);
		//Load Library
		$this->load->library('user_agent');
		//Load model
		$this->load->model('award_model');
		//Set Templates
		$this->folder_templates = "award/";
      	//Set Script Default
   		$this->data['js']    = array('jquery-1.8.2.min.js','awardScript.js');
    	//Set Style Default
    	$this->data['css']   = array(''.(!ONIDEV ? 'awardStyle.min.css':'awardStyle.css').'');	
        //Set Script External Default	
        $this->data['external_js']  = '';
        //Set style External Default	
        $this->data['external_css'] = '';
		//Set url
		$this->data['main_url']  = "http://www.you2play.com/";	
		$this->data['award_url']  = "http://award.you2play.com/";	
		//Set Asset Path		
		$this->data['asset_award_upload_path']  = "../uploads/award/";
		//Set Asset Image Path		
		$this->data['asset_award_image_path']  = "award/";				
		$this->data['asset_award_artis_path']  = "artist2014/";				
		//Set Upload Folder
		$this->upload_folder = (!ONIDEV ? $this->config->item('path_asset_url').'assets/uploads/':realpath('assets/uploads/').'/');
		$this->expireShowPercent = strtotime('2015-02-03 23:59:59');
		$this->expireMent = strtotime('2015-02-09 23:59:59');
		$this->expireActivity = strtotime('2014-02-10 23:59:59');
		$this->showWinner = strtotime('2015-02-10 23:59:59');
		//Set Category
		$this->data['dataCategory'] = array(
			'01' => 'ศิลปินชายยอดนิยม (Favorite Male Artist)',
			'02' => 'ศิลปินหญิงยอดนิยม (Favorite Female Artist)',
			'03' => 'ศิลปินหน้าใหม่ยอดนิยม (Favorite New Artist)',
			'04' => 'ศิลปินทีนป็อบยอดนิยม (Favorite Teen Pop Artist)',
			'05' => 'ศิลปินคู่หรือกลุ่มยอดนิยม (Favorite Duo/Group Artist)',
			'06' => 'มิวสิควีดีโอเพลงไทยยอดนิยม (Favorite Thai Music Video)',
			'07' => 'ศิลปินเพลงอิเล็กโทรนิคส์แดนซ์ต่างประเทศ (Favorite International Electronic Dance Artist)',
			'08' => 'ศิลปินเอเชียนยอดนิยม (Favorite Asian Artist)',
			'09' => 'มิวสิควีดีโอเพลงเอเชียนยอดนิยม (Favorite Asian Music Video)',
			'10' => 'ศิลปินต่างประเทศยอดนิยม (Favorite International Artist)',
			'11' => 'มิวสิควีดีโอต่างประเทศยอดนิยม (Favorite International Music Video)',
			'12' => 'รายการทางอินเทอร์เน็ตยอดนิยม (Favorite Internet Program)'
		);
		//Set Color
		$this->data['dataCategoryColor'] = array(
			'',
			'#1d3565',
			'#310d31',
			'#f8981d',
			'#f46722',
			'#6866af',
			'#14996a',
			'#1d3565',
			'#310d31',
			'#f8981d',
			'#f46722',
			'#6866af',
			'#14996a'
		);
		
		//Popup Page		
		$this->data['active_popup_page'] = true;
		$this->data['active_popup_page_timeout'] = 36000; //Every 1 hour (60 * 60 * 1 = 36000)
		if($this->data['active_popup_page'] && !$this->agent->is_robot()):
			if(abs($this->input->cookie('you2play_award_popup') - time()) > $this->data['active_popup_page_timeout']):
		      	//Set Script Default
		   		$this->data['js'][]    = 'jquery.colorbox-min.js';
		    	//Set Style Default
		    	$this->data['css'][]   = 'colorbox.css';
				$mycookie = array(
					'name'  => 'you2play_award_popup',
					'value' => time(),
					'expire'=> ''.$this->data['active_popup_page_timeout'].''
				);
				set_cookie($mycookie);
				$this->data['active_popup_page'] = true;
			elseif($this->session->userdata('fb_id') && (abs($this->input->cookie('you2play_award_member_popup') - time()) > $this->data['active_popup_page_timeout'])): //Member
		      	//Set Script Default
		   		$this->data['js'][]    = 'jquery.colorbox-min.js';
		    	//Set Style Default
		    	$this->data['css'][]   = 'colorbox.css';			
				$mycookie = array(
					'name'  => 'you2play_award_member_popup',
					'value' => time(),
					'expire'=> ''.$this->data['active_popup_page_timeout'].''
				);
				set_cookie($mycookie);
				$this->data['active_popup_page'] = true;			
			else:
				$this->data['active_popup_page'] = false;
			endif;
		else:
			$this->data['active_popup_page'] = false;				
		endif;									
		//Load Library
		//$this->load->library('pagination');	
    }
	
	/*FaceBook*/
	public function get_user_fb()
	{
		$fb_id = $this->input->post('user_id');
		$username = $this->input->post('user_name');
		$datetime = date('Y-m-d H:i:s');
		//Add database
		$is_exist = $this->db->from('member')->where(array('fb_id' => $fb_id))->count_all_results();
		if(empty($is_exist) && !empty($fb_id)):
			$this->DB_MAIN = $this->load->database("you2play_2013",TRUE);
			//Member
			$data = array(
				'fb_id' => $fb_id,
				'status' => '1',
				'is_newsletter' => '1',
				'last_ip_address' => $this->input->ip_address(),
				'create_date' => $datetime,
				'update_date' => $datetime
			);
			$this->DB_MAIN->insert('member',$data);
			$member_id = $this->DB_MAIN->insert_id();
			//Member Info
			$data = array(
				'member_id' => $member_id,
				'username' => $username
			);
			$this->DB_MAIN->insert('member_info',$data);	
			$this->DB_MAIN->close();
			//Load Database award again
			$this->DB_AWARD = $this->load->database("you2play_award_2014",TRUE);	
		endif;

		if(!empty($fb_id)):
			$data = array(
				'fb_id' => $fb_id,
				'fb_name' => $username
			);
			$this->session->set_userdata($data);
		else:
			$array_items = array('fb_id' => '', 'fb_name' => '');
			$this->session->unset_userdata($array_items);
		endif;		
	}
	
	public function delete_user_fb()
	{
		$data = array('fb_id' => '', 'fb_name' => '');
		$this->session->unset_userdata($data);
	}
	
	/*Close Popup*/
	public function close_popup()
	{
		echo "<script>close();</script>";
		exit;
	}
	
	/*Home*/	 
	public function index()
	{
		//Load Javascript
		$this->data['js'][] = "jquery.multipleelements.cycle.min.js";	

		//Load Media Player
		if($this->agent->is_mobile()):
			$this->data['js'][] = "media/player.js";
		else:
			$this->data['js'][] = "media/jwplayerx.js";
		endif;
	
		$this->data['content'] = preg_replace("/_/","/",$this->router->class)."/index";
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);
	}
	
	/*Show*/
	public function show()
	{
		redirect('');
		exit;		
		//Load Media Player
		if($this->agent->is_mobile()):
			$this->data['js'][] = "media/player.js";
		else:
			$this->data['js'][] = "media/jwplayerx.js";
		endif;
			
		$this->data['content'] = preg_replace("/_/","/",$this->router->class)."/".$this->router->method;
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
	}
	
	/*Vote*/
	public function vote()
	{
		//Load Media Player
		if($this->agent->is_mobile()):
			$this->data['js'][] = "media/player.js";
		else:
			$this->data['js'][] = "media/jwplayerx.js";
		endif;
	
		$time_limit = time()-(60*30); //last 1/2 hours 
		//$this->data['dataVote'] = json_decode(file_get_contents($this->upload_folder.''."/award/vote/vote.json"));
		$this->data['dataVote'] = $this->DB_AWARD->get('award_vote_score',1)->row();
		$dataResult = $this->DB_AWARD->select('cat,at')->get_where('award_vote',"user_id = '".$this->session->userdata('fb_id')."' AND unix_timestamp(fb_time) > '".$time_limit."'")->result();
		foreach($dataResult as $value):
			$this->data['cat'.$value->cat] = $value->at;
		endforeach;
		
		$cat_total = count($this->data['dataCategory']);
		for($i=1;$i<=$cat_total;$i++):
			for($j=1;$j<=6;$j++):
				$this->data['dataXML'.sprintf('%02d',$i).sprintf('%02d',$j)] = simplexml_load_file($this->upload_folder.'award/'.$this->data['asset_award_artis_path'].sprintf('%02d',$i)."/".sprintf('%02d',$j)."/data.xml");
			endfor;
		endfor;
		
		$this->data['content'] = preg_replace("/_/","/",$this->router->class)."/".$this->router->method;
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);
	}	
	
	public function ajax_vote()
	{	
		$this->award_model->ajax_vote();
	}
	
	/*Ment*/
	public function ment()
	{
		//Load Javascript
		$this->data['js'][] = "jquery.multipleelements.cycle.min.js";
		
		//GET Result in 12 Category
		$this->data['dataResult'][] = array();
		for($i=1;$i<=12;$i++):
			$this->data['dataResult'][] = $this->DB_AWARD->select('*,MAX(likes) as likes')->group_by('user_id')->order_by('likes DESC')->get_where('award_comment',array('is_active' => '1','cat_id' => $i),5)->result();
		endfor;
			
		$this->data['content'] = preg_replace("/_/","/",$this->router->class)."/".$this->router->method;
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
	}
	
	/*Artist*/
	public function artist2014($cat,$at)
	{
		//Load Javascript
		$this->data['js'][] = "jquery.multipleelements.cycle.min.js";
			
		//Load Media Player
		if($this->agent->is_mobile()):
			$this->data['js'][] = "media/player.js";
		else:
			$this->data['js'][] = "media/jwplayerx.js";
		endif;	
	
		$this->data['folder_path'] =  $this->data['asset_award_upload_path'].$this->data['asset_award_artis_path'].$cat."/".$at;
		$this->data['fb_image'] =  $this->data['asset_award_upload_path'].$this->data['asset_award_artis_path'].$cat."/".$at."/artist_left_panel.jpg";
		$time_limit = time()-(60*30); //last 1/2 hours 
		$this->data['is_vote'] = $this->DB_AWARD->from('award_vote')->where("user_id = '".$this->session->userdata('fb_id')."' AND cat='".$cat."' AND unix_timestamp(fb_time) > '".$time_limit."'")->count_all_results();
		$this->data['dataXML'] = simplexml_load_file($this->upload_folder.''.$this->data['folder_path']."/data.xml");
			
		$this->data['content'] = preg_replace("/_/","/",$this->router->class)."/artist";
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
	}
	
	/*Video*/
	public function video()
	{
		//Load Media Player
		if($this->agent->is_mobile()):
			$this->data['js'][] = "media/player.js";
		else:
			$this->data['js'][] = "media/jwplayerx.js";
		endif;
		
		/*Load Video XML*/
		$this->data['dataXML'] = simplexml_load_file($this->upload_folder."award/video/data.xml");

		$this->data['content'] = preg_replace("/_/","/",$this->router->class)."/".$this->router->method;
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
	}	
	
	/*Photo*/
	public function photo()
	{
		//Load Javascript
		$this->data['js'][] = "jquery.multipleelements.cycle.min.js";

		/*Load Colorbox*/
   		$this->data['js'][]    = "jquery.colorbox-min.js";
    	$this->data['css'][]   = "colorbox.css";
					
		//Load Gallery
		$this->data['dataGallery'] = directory_map($this->upload_folder.$this->data['asset_award_upload_path'].'photo',2);
		
		$this->data['content'] = preg_replace("/_/","/",$this->router->class)."/".$this->router->method;
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);				
	}
	
	/*News*/
	public function news()
	{
		redirect('');
		exit;		
		//Load Javascript
		$this->data['js'][] = "jquery.multipleelements.cycle.min.js";

		/*Load Colorbox*/
   		$this->data['js'][]    = "jquery.colorbox-min.js";
    	$this->data['css'][]   = "colorbox.css";
					
		/*Load Video XML*/
		$this->data['dataXML'] = simplexml_load_file($this->upload_folder."award/news/data.xml");
		
		$this->data['content'] = preg_replace("/_/","/",$this->router->class)."/".$this->router->method;
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);				
	}	
	
	/*About*/
	public function about()
	{
		redirect('');
		exit;
		//Load Media Player
		if($this->agent->is_mobile()):
			$this->data['js'][] = "media/player.js";
		else:
			$this->data['js'][] = "media/jwplayerx.js";
		endif;
			
		$this->data['content'] = preg_replace("/_/","/",$this->router->class)."/".$this->router->method;
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
	}
	
	/*Mail*/
	public function mail_to_admin()
	{		
		//Get Contact
		$dataContact = $this->db->get('contact_us',1)->row();
		$dataResult->reciever_name = 'PMD+ CO.,Ltd';
		$dataResult->receiver_email = 'vorawatc@pmdplus.com';
		$dataResult->receiver_subject = 'Onisueka';
		$dataResult->reciever_message = 'ทดสอบ';
		$dataResult->receiver_email = $contact_email;
		$dataResult->sender_name = 'Develop You2play';

		//Send Email
		sendEmail($dataResult);	
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */